
.DEFAULT_GOAL = rbuild

RUN ?= tb_ovip_xdata
SIMULATOR ?= verilator
TRACE ?= OFF
WAVE_FORMAT ?= VCD


init:
	rm -rf dut && mkdir dut
	git submodule update --init --recursive
	$(build_mcv)
	$(build_mcv-ut)


gstat:
	git status
	cd mcv-ut && git status
	cd open-vip && git status

gpull:
	git pull
	cd mcv-ut && git pull
	cd open-vip && git pull

gcheckout:
	git checkout .
	cd mcv-ut && git checkout .
	cd open-vip && git checkout .

gadd:
	cd mcv-ut && git add .
	cd open-vip && git add .
	git add .

gcommit:
	cd mcv-ut && git commit -m "${MSG}"
	cd open-vip && git commit -m "${MSG}"
	git commit -m "${MSG}"

gpush:
	cd mcv-ut && git push
	cd open-vip && git push
	git push

run: build
	
	@echo "run test:"
	@if [ "${TRACE}" == "ON" ]; then \
		time ./build/bin/${RUN}_trace;	\
	else \
		time ./build/bin/${RUN}; \
	fi

build:
	# build_mcv-ut dir
	@mkdir -p dut
	if [ ! -d "mcv-ut/build" ]; then \
		$(build_mcv-ut); \
	fi
	cmake . -Bbuild -DCMAKE_BUILD_TYPE=Debug -DSIMULATOR=${SIMULATOR} -DTRACE=${TRACE} -DWAVE_FORMAT=${WAVE_FORMAT} -DRUN=${RUN} 
	cd build && make -j`nproc`

clean:
	rm -rf build

rbuild: clean build run

# Unit Tests

uvm-cache_%:
	@$(MAKE) -C src/verilog/UVM/uvm-cache $(subst uvm-cache_,,$@)

.PHONY: uvm-cache

define build_mcv
	@cd mcv && rm -rf build && cmake . -Bbuild \
		&& cd build && make -j`nproc`
	@if ! command -v verible-verilog-format ; then \
		echo "verible-verilog-format could not be found"; \
		echo "Please install verible-verilog-format"; \
    	echo "https://github.com/chipsalliance/verible/releases/tag/v0.0-3428-gcfcbb82b"; \
		exit 1; \
	fi
endef

define build_mcv-ut
	cd mcv-ut && rm -rf build && make all -j`nproc`
endef